package com.syy.spider.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.syy.spider.dao.NoticeMapper;
import com.syy.spider.dao.UserMapper;
import com.syy.spider.pojo.Admin;
import com.syy.spider.pojo.NoticeGet;
import com.syy.spider.pojo.User;
import com.syy.spider.service.AdminService;
import com.syy.spider.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private AdminService adminService;

    @Autowired
    private LoginService loginService;
    @RequestMapping("adminComment")
    private String adminComment(){
        return "admin/comment";
    }
    @RequestMapping("adminNotice")
    public String toNotice(){
        return "admin/notice";
    }

    @RequestMapping("toEditNotice")
    public String toEditNotice(){
        return "admin/editNotice";
    }

    @RequestMapping("toFindAllUsers")
    public String toFindAllUsers(){
        return "admin/userManage";
    }

    @RequestMapping("addNoticeHtml")
    public String toAddNotice(){
        return "admin/addNotice";
    }

    @RequestMapping(value = "/adminLogin",method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView login(HttpServletRequest request, HttpSession session){
        ModelAndView mav = new ModelAndView();
        Admin admin = new Admin();
        String username = request.getParameter("adminName");
        System.out.println("用户名:"+username);
        admin.setUsername(username);
        String password = request.getParameter("password");
        admin.setPassword(password);
        boolean loginResult = loginService.adminLogin(admin);
        if (loginResult){
            mav.setViewName("admin/notice");
            session.setAttribute("admin",admin);
        }else {
            mav.setViewName("login/loginFail");
        }
        return mav;
    }

    @RequestMapping(value = "/getAdminName")
    public void getUserName(HttpServletRequest request, HttpServletResponse response){
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.writeValue(response.getOutputStream(),admin);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/adminHeadHtml")
    public String headHtml() {
        return "admin/header";
    }

    @RequestMapping(value = "/addNotice",method = {RequestMethod.POST, RequestMethod.GET})
    public void addNotice(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> params){
        String title = (String)params.get("title");
        String con = (String)params.get("con");
        String date = (String)params.get("date");
        NoticeGet noticeGet = new NoticeGet();
        noticeGet.setTitle(title);
        noticeGet.setCon(con);
        try {
            noticeGet.setDate(new SimpleDateFormat("yyyy-MM-dd'T'hh:mm").parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int a = noticeMapper.addNewNotice(noticeGet.getTitle(),noticeGet.getCon(),noticeGet.getDate());
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.writeValue(response.getOutputStream(),"true");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/deleteNotice")
    @ResponseBody
    public void deleteNotice(HttpServletRequest request, HttpSession session,@RequestBody Map<String, Object> params){
        int id = (int) params.get("id");
        int a = noticeMapper.deleteNoticeById(id);
    }

    @RequestMapping("/getEditNotice")
    public void getEditNotice(HttpServletRequest request, HttpServletResponse response,@RequestBody Map<String, Object> params){
        int id = Integer.parseInt((String)params.get("id"));
        NoticeGet noticeGet = noticeMapper.selectById(id);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.writeValue(response.getOutputStream(),noticeGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/editNotice",method = {RequestMethod.POST, RequestMethod.GET})
    public void editNotice(HttpServletRequest request, HttpServletResponse response,@RequestBody Map<String, Object> params){
        int id = Integer.parseInt((String)params.get("id"));
        String title = (String)params.get("title");
        String con = (String)params.get("con");
        String date = (String)params.get("date");
        NoticeGet noticeGet = new NoticeGet();
        noticeGet.setTitle(title);
        noticeGet.setCon(con);
        try {
            noticeGet.setDate(new SimpleDateFormat("yyyy-MM-dd'T'hh:mm").parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int a = noticeMapper.editNotice(noticeGet.getTitle(),noticeGet.getCon(),noticeGet.getDate(),id);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.writeValue(response.getOutputStream(),"true");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/getUsers")
    @ResponseBody
    public List<User> getUsers(){
        return userMapper.getAllUsers();
    }

    @RequestMapping("/toSelf")
    public String toAdminSelf(){
        return "admin/self";
    }

    @RequestMapping("/toAdminChangePwd")
    public String toChange(){
        return "admin/changePwd";
    }

    @PostMapping("/adminChangePassword")
    @ResponseBody
    public int changePassword(HttpServletRequest request, HttpSession session,@RequestBody Map<String, Object> params){
        System.out.println("进入修改密码方法");
        String oldPas =(String) params.get("oldPas");
        System.out.println("旧密码"+oldPas);
        String newPas =(String) params.get("newPas");
        System.out.println("新密码"+newPas);
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        String username = admin.getUsername();
        System.out.println("用户名"+username);
        Admin getUser = adminService.selectUserByNameAndPas(oldPas,username);
        if (getUser!=null){
            if (!oldPas.equals(newPas)){
                adminService.updatePwd(username,newPas);
                return 1;
            }else {
                return 2;
            }
        }else {
            return 3;
        }
    }
}
